package gzhweichat

import (
	"crypto/sha1"
	"encoding/hex"
	"fmt"
	"main.go/shttps"
	"math/rand"
	"strconv"
	"time"
)

//公众号逻辑层


//const GzhAppID = "wx36c6b2b48918720f"
//const GzhAppSecret = "4f503b6edddfc60aec04e861e694ebb6"
////微信
//var Access_token string//通过获取openid获得
//var Ticket string//jsd签名用的，全局存储
//var Expires_in int //过期时间
//var TExpires_in int //ticket的过期时间
//var Noncestr string //随机字符串
//var Timestamp int64 //配置的时间戳
//var XTTime int //系统的时间，用来定时执行，过期了就执行一次
//公众号逻辑处理

//获取getopenid
func GetOpenid(code string)(*TokenRes,error){
	url:="https://api.weixin.qq.com/sns/oauth2/access_token"+"?appid="+GzhAppID+"&secret="+GzhAppSecret+"&code="+code+"&grant_type=authorization_code"
	tokenres:=new(TokenRes)

	//获取token
	err:=shttps.New(url).Res(&tokenres).Post()
	if err!=nil {
		return nil, err
	}
	return tokenres, nil
}

//拉去用户部分信息
func GetUserInfo(openid string ,access_token string)(*WxUserInfoRes,error){

	url:="https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openid+"&lang=zh_CN"

	userinfores:=new(WxUserInfoRes)

	err:=shttps.New(url).Res(&userinfores).AddHead("access_token",access_token).AddHead("Authorization",access_token).Get()
	if err!=nil {
		return nil, err
	}
	return userinfores, nil
}

//获取jsapi_ticket用于签名 ,前提是全局变量Access_token 不为空 当全局变量的token更新后，获取ticket信息
func GetTicket()(*TicketRes,error,string){
	//https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=ACCESS_TOKEN&type=jsapi
	url:="https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="+Access_token+"&type=jsapi"

	Tickerres:=new(TicketRes)
	err:=shttps.New(url).Res(&Tickerres).Get()
	if err!=nil {
		return nil, err,url
	}
	return Tickerres, nil,url

}

//获取全局变量的token
func Alltoken()(error){
	url:="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+GzhAppID+"&secret="+GzhAppSecret
	alltoken:=new(AllToken)

	err:=shttps.New(url).Res(&alltoken).Get()
	if err!=nil {
		return err
	}

	//更新token值
	Access_token=alltoken.Access_token
	Expires_in=alltoken.Expires_in

	//ticket和token的过期时间都是7200秒
	//所以要更新就一起更新参数吧
	//存储 全局变量ticket
	Ticketinfo,_,_:=GetTicket()
	Ticket=Ticketinfo.Ticket
	TExpires_in=Ticketinfo.ExpiresIn
	XTTime=0//初始化

	fmt.Println("Access_token",Access_token)
	fmt.Println("Expires_in",Expires_in)

	return nil
}

//生成公众号签名 url :(当前网页的URL，不包含#及其后面部分)
func Sign(htmlurl string)(string){
	//按ASCLL从小到大排序  根据字段名
	//不想找，直接写死，顺序：jsapi_ticket，noncestr，timestamp，url
	//key1=value1&key2=value2…,
	//然后进行sha1加密,得到签名

	//更新随机字符串和时间戳
	Noncestr=RandStringRunes(32)
	Timestamp=time.Now().Unix()
	tstr:=strconv.FormatInt(Timestamp,10) //时间戳转字符串

	//拼接
	str:="jsapi_ticket="+Ticket+"&noncestr="+Noncestr+"&timestamp="+tstr+"&url="+htmlurl
	signstr:=SHA1(str)

	return signstr
}

//sha1 加密
func SHA1(s string) string {
	o := sha1.New()
	o.Write([]byte(s))
	return hex.EncodeToString(o.Sum(nil))

}

//RandStringRunes 生成随机字符串
func RandStringRunes(n int) string {
	letterRunes := []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

	b := make([]rune, n)
	for i := range b {
		b[i] = letterRunes[rand.Intn(len(letterRunes))]
	}
	return string(b)
}

